package com.nineclock.common.exception;

import com.nineclock.common.entity.Result;
import com.nineclock.common.exception.NcException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author yangchao
 * @create 2022-01-12 17:37
 **/
@ControllerAdvice
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 抓取 CheckCodeException(验证码异常)
     *
     * ExceptionHandler 表示要抓异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(CheckCodeException.class)
    public Result catchException(CheckCodeException e){

        log.info(e.getMessage());
//        return Result.errorCodeMessage(e.getCode(),e.getMessage());
        return Result.diyResult(true,e.getCode(),e.getMessage(),null);

    }

    /**
     * 抓取 NcException
     *
     * ExceptionHandler 表示要抓异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(NcException.class)
    public Result catchException(NcException e){

        log.info(e.getMessage());
        return Result.errorCodeMessage(e.getCode(),e.getMessage());

    }

    /**
     * 抓取 Exception
     *
     * ExceptionHandler 表示要抓异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result catchException2(Exception e){

        log.info(e.getMessage());
        return Result.errorMessage(e.getMessage());

    }

}
